package cn.itlaobing.spring.web.controller;

import java.text.ParseException;

import javax.validation.Valid;

import org.apache.commons.lang3.time.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import cn.itlaobing.spring.entity.Student;
import cn.itlaobing.spring.entity.enums.Gender;
import cn.itlaobing.spring.service.StudentsService;

@Controller
@RequestMapping("/students")
public class StudentsController extends BaseController{
	
	@Autowired
	private StudentsService studentsService;
	
	/**
	 * 列出所有用户
	 * @return
	 */
	@RequestMapping(method=RequestMethod.GET)
	public String index(Model model) {
		model.addAttribute("students", studentsService.findAll());
		return "student/index";
	}
	
	/**
	 * 	呈现新建用户的表单
	 * @return
	 */
	@GetMapping("/create")
	public String _new(Model model) {
		Student student=new Student();
		model.addAttribute("student",student);	
		model.addAttribute("genders", Gender.values());
		return "student/create";
	}
	
	/**
	 * 保存到数据库，create方法执行完毕后进行重定向，以免发生重复提交
	 * @return
	 */
	@RequestMapping(value="/create",method=RequestMethod.POST)
	public String create(@Valid @ModelAttribute Student student,BindingResult bindingResult,RedirectAttributes redirectAttributes,Model model) {
		if(bindingResult.hasErrors()){
			model.addAttribute("genders", Gender.values());
			return "student/create";
		}
		studentsService.save(student);
		redirectAttributes.addFlashAttribute("toastMsg", "保存"+student.getName()+"成功！");
		return "redirect:/students";
	}
	
	/**
	 * 显示指定id的信息
	 * @return
	 */
	@RequestMapping(value="/{id}",method=RequestMethod.GET)
	public ModelAndView show(@PathVariable Long id) {
		Student student=studentsService.findAllById(id);
		ModelAndView modelAndView=new ModelAndView();
		modelAndView.addObject("student", student);
		modelAndView.setViewName("student/show");
		return modelAndView;
	}
	
	/**
	 * 显示编辑页面
	 * @return
	 */
	@RequestMapping(value="/{id}/update",method=RequestMethod.GET)
	public String edit(@PathVariable Long id,Model model) {
		Student student=studentsService.findAllById(id);
		model.addAttribute("student",student);	
		model.addAttribute("genders", Gender.values());
		return "student/edit";
	}
	
	/**
	 * 更新到数据库，update方法执行完毕后进行重定向，以免发生重复提交
	 * @return
	 */
	@RequestMapping(value="/{id}/update",method=RequestMethod.POST)
	public String update(@Valid @ModelAttribute Student student,BindingResult bindingResult,RedirectAttributes redirectAttributes,Model model) {
		if(bindingResult.hasErrors()){
			model.addAttribute("genders", Gender.values());
			return "student/create";
		}
		studentsService.update(student);
		redirectAttributes.addFlashAttribute("toastMsg", "修改"+student.getName()+"成功！");
		return "redirect:/students";
	}
	
	/**
	 * 从数据库中删除，destory方法执行完毕后进行重定向，以免发生重复提交
	 * @return
	 */
	@RequestMapping(value="/{id}",method=RequestMethod.DELETE)
	@ResponseBody
	public Student destory(@PathVariable Long id) {
		Student student=studentsService.findAllById(id);
		studentsService.delete(id);
		log.info(id);
		return student;
	}
	
	@RequestMapping("/json")
	@ResponseBody
	public Student name() {
		Student student=new Student();
		student.setId(1);
		student.setName("张三");
		try {
			student.setBirthday(DateUtils.parseDate("2015-02-04","yyyy-MM-dd"));
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return student;
	}
}
